package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author stone
 * @date 2022/09/03 14:43
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
public class ExceptionControllerAdvice {

    //@ExceptionHandler(value = {ArithmeticException.class})
    /*@ExceptionHandler(ArithmeticException.class)
    public ModelAndView arithmeticExceptionResolve(ArithmeticException exception) {//方法的形参可以写Exception
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/byzero.jsp");
        return modelAndView;
    }*/

    @ExceptionHandler(ArithmeticException.class)
    //@ResponseBody
    public BaseRespVo arithmeticExceptionResolve(ArithmeticException exception) {
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setMsg("除零异常:" + exception.getMessage());
        baseRespVo.setStatus(500);
        return baseRespVo;
    }
}
